﻿using System;
using System.Collections.Generic;
using System.Text;
using NExt;
using NExt.form;
using NExt.UI.Base.Data.Controller;
using NExt.UI.Resource.Icon;
using NExt.Core.Model.Base;
using NExt.UI.Base.Js;
using NExt.UI.Base.Right;

namespace NExt.UI.Edit.Base
{
    public class EditPresenterBase : AbstractEditPresenter
    {
        public Toolbarr toolbar;
        public Toolbar.Button tbbSave;
        public Toolbar.Button tbbExit;
        public Toolbar.Button tbbCancel;
        public FormPanel formPanel;
        public IRightSetterScript rightSetter { get; set; }
        public IDataControllerScript dataController { get; set; }

        protected IScript onTbbSaveInvoked;
        protected IScript onTbbExitInvoked;
        protected IScript onTbbCancelInvoked;

        protected readonly static IScript fireSave = new ExternalJs("fireSave");
        protected readonly static IScript fireExit = new ExternalJs("fireClose");
        protected readonly static IScript fireCancel = new ExternalJs("fireCancel");

        protected override void FormBuildComponents()
        {
            base.FormBuildComponents();
            this.FormBuildRightSetter();
            this.FormBuildDataController();
            toolbar = new Toolbarr();
            tbbSave = new Toolbar.Button();
            tbbExit = new Toolbar.Button();
            tbbCancel = new Toolbar.Button();
            formPanel = new FormPanel();
        }

        protected virtual void FormBuildRightSetter()
        {
            rightSetter = new NullRightSetter();
        }

        protected virtual void FormBuildDataController()
        {
            dataController = new NullDataController();
        }

        protected override void FormInitializeEvents()
        {
            base.FormInitializeEvents();
            onTbbSaveInvoked = fireSave;
            onTbbExitInvoked = fireExit;
            onTbbCancelInvoked = fireCancel;
        }

        protected override void FormInitializeComponents()
        {
            base.FormInitializeComponents();

            tbbSave.text = "Save";
            tbbSave.iconCls = IconCls.Save16;
            tbbSave.listeners.click = onTbbSaveInvoked;
            toolbar.buttons.Add(tbbSave);

            tbbCancel.text = "Cancel";
            tbbCancel.iconCls = IconCls.Cancel16;
            tbbCancel.listeners.click = onTbbCancelInvoked;
            toolbar.buttons.Add(tbbCancel);

            tbbExit.text = "Exit";
            tbbExit.iconCls = IconCls.Exit16;
            tbbExit.listeners.click = onTbbExitInvoked;

            toolbar.buttons.Add(tbbExit);

            formPanel.border = false;

            items.Add(toolbar);
            items.Add(formPanel);
        }
    }
}
